//
//  LXHotMusicTableVC.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LXHotMusicTableVC.h"

@interface LXHotMusicTableVC ()
@property(nonatomic,strong)NSMutableArray *allArray;
@end

@implementation LXHotMusicTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    [self createNavigation];
    [self getData];
    [self createTableView];
}
-(void)getData{
//    NSLog(@"%@",self.contentType);
//    NSLog(@"%@",self.key);
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=iphone&key=%@&pageId=1&pageSize=40", self.contentType, self.key];
    [AFNetworkTool getUrl:urlStr body:nil result:HXJSON headerFile:nil success:^(id result) {
        NSArray *array = result[@"list"];
        for (NSDictionary *dic in array) {
            LXHotMusicModel *temp = [LXHotMusicModel new];
            [temp setValuesForKeysWithDictionary:dic];
            CGSize labelSize = [AppTools labelRectWithLabelSize:CGSizeMake(SCREEN_WIDTH-140, 10000) LabelText:temp.title Font:[UIFont systemFontOfSize:18]];
            temp.height = labelSize.height;
            [self.allArray addObject:temp];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
    }];

}
-(void)createTableView{
    [self.tableView registerClass:[LXHotMusicTableViewCell class] forCellReuseIdentifier:@"cellMusic"];
}
-(void)createNavigation{
    self.navigationItem.title = self.NavigationTitle;
    UIImage *left = [UIImage imageNamed:@"icon_back_h@2x.png"];
    left = [left imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:left style:UIBarButtonItemStyleDone target:self action:@selector(leftAction)];
}
-(void)leftAction{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _allArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LXHotMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellMusic" forIndexPath:indexPath];
    cell.index = indexPath.row;
    cell.cellModel = [_allArray objectAtIndex:indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LXHotMusicModel *model = [_allArray objectAtIndex:indexPath.row];
    LXMusicPlayerVC *music = [LXMusicPlayerVC new];
    music.Title = model.title;
    music.headImageLarge = model.coverSmall;
    music.songId = model.trackId;
    music.index = indexPath.row;
    music.allArray = [NSArray arrayWithArray:_allArray];
    music.totaltime = model.duration;
    [self presentViewController:music animated:YES completion:nil];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    LXHotMusicModel *temp = [_allArray objectAtIndex:indexPath.row];
    if (temp.height + 10 + 10 + 5 < 80) {
        return 80;
    }else{
    return temp.height + 10 + 10 + 5 +15 ;
    }
}
@end
